// pages/address/address.js
const app = getApp();
const allcity = require('../../utils/allcity.js');
import ajax from "../../utils/ajax.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityGroup: allcity,
    isSearch: false,
    searchText: '',
    searchArrGroup: [],
    type:'',
    Tindex:0,
    list:[],
    hot:[],
    now_city:{
      name:'定位中..'
    }
  },
  goback: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  // 搜索输入
  inputSearchText: function (e) {
    this.setData({
      searchText: e.detail.value
    });
  },
  // 搜索
  searchCity: function () {
    let _cityGroup = this.data.cityGroup;
    let _searchText = this.data.searchText;
    let tempArr = [];
    _cityGroup.forEach(function (parentItem, index) {
      parentItem.item.forEach(function (item, index) {
        if (item.name.indexOf(_searchText, 0) >= 0) {
          tempArr.push(item);
        }
      });
    });
    this.setData({
      isSearch: true,
      searchArrGroup: tempArr
    })
  },
  // 城市选择
  chooseCity: function (e) {
 
    let name = e.currentTarget.dataset.name;
    let id = e.currentTarget.dataset.id;
    let spell = e.currentTarget.dataset.spell;
    let data = {
      name,id,spell
    }
    wx.setStorageSync("select_city_data", data)
    // wx.navigateBack({})
    //获取已经打开的页面的数组
    var  pages = getCurrentPages();
    //获取上一个页面的所有的方法和data中的数据
    var  lastpage = pages[pages.length -  2]  
    lastpage.updata_city_data(data);
    // //调用上一个页面中的changeData方法
    // lastpage.changecity(cityName,this.data.type,this.data.Tindex)
    // app.data.address.name = cityName
    wx.navigateBack({})
  },
  /**
 * 生命周期函数--监听页面加载
 */
get_city_list(){
  ajax.get({
    url:'lbs/hot_city_list'
  }).then(res=>{
    res = res.data.list_data;
    // console.log(res);
    let hot = res[res.length-1];
    res.pop();
    this.setData({
      list:res,
      hot
    })
  })
},
get_location_to_city(){
  let  that = this;
  wx.getLocation({
    type: 'wgs84',
    success (res) {
      let latitude = res.latitude
      let longitude = res.longitude
      ajax.get({
        url:'lbs/location',
        data:{
          latitude,
          longitude
        }
      }).then(res=>{
        // console.log(res);
        that.setData({
          now_city:res.data
        })
      })
    }
   })
},
  onLoad: function (options) {
    this.get_city_list();
    this.get_location_to_city();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})